<?php
include("include/config.inc.php");

if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  $msg = "Record Not Found";
  
  if(isset($_POST['notOkButton']))
  {
//  	echo "<pre>";
//  	print_r($_POST);
//  	$_POST['planningId']
//  	$_POST['logSheetId']
//  	$_POST['processNo']
//  	$_POST['status']
//  	$_POST['process1Reorder']
//  	$_POST['process2Reorder']
//  	$_POST['process3Reorder']
//  	$_POST['notOkReason']
  	
	  $selPlanning = "SELECT incomingId, partyId, date, quantity, weight, processId1, processId2, processId3, noOfProcess,
	                         temp1, soaking1, furnace1, temp2, soaking2, furnace2, temp3, soaking3, furnace3,
	                         okNotOk1, okNotOk2, okNotOk3, status, parentPlanningId
	                    FROM planning
	                   WHERE planningId =".$_POST['planningId'];
    $selPlanningRes = mysql_query($selPlanning);
    if($planRow = mysql_fetch_array($selPlanningRes))
    {
    	$process1Reorder = isset($_POST['process1Reorder']) && $_POST['process1Reorder'] > 0 ? -1 : $planRow['okNotOk1'];
    	$process2Reorder = isset($_POST['process2Reorder']) && $_POST['process2Reorder'] > 0 ? -1 : $planRow['okNotOk2'];
    	$process3Reorder = isset($_POST['process3Reorder']) && $_POST['process3Reorder'] > 0 ? -1 : $planRow['okNotOk3'];
    	
    	if($process1Reorder != -1)
    	{
    		$temp1    = $planRow['temp1'];
    		$soaking1 = $planRow['soaking1'];
    		$furnace1 = $planRow['furnace1'];
    	}
      else
      {
    		$temp1    = 0;
    		$soaking1 = 0;
    		$furnace1 = 0;
      }    	
    	if($process2Reorder != -1)
    	{
    		$temp2    = $planRow['temp2'];
    		$soaking2 = $planRow['soaking2'];
    		$furnace2 = $planRow['furnace2'];
    	}
      else
      {
    		$temp2    = 0;
    		$soaking2 = 0;
    		$furnace2 = 0;
      }    	
      
    	if($process3Reorder != -1)
    	{
    		$temp3    = $planRow['temp3'];
    		$soaking3 = $planRow['soaking3'];
    		$furnace3 = $planRow['furnace3'];
    	}
      else
      {
    		$temp3    = 0;
    		$soaking3 = 0;
    		$furnace3 = 0;
      }    	

    	if($planRow['date'] == NULL)
    	{
    		$planRow['date'] = date('Y-m-d');
    	}
    	$insNewRecord = "INSERT INTO planning (incomingId, partyId, date, quantity, weight, processId1, processId2, processId3, noOfProcess, 
    	                                       temp1, soaking1, furnace1, temp2, soaking2, furnace2, temp3, soaking3, furnace3, 
    	                                       okNotOk1, okNotOk2, okNotOk3,status, parentPlanningId)
         	                   VALUES (".$planRow['incomingId'].",".$planRow['partyId'].",'".$planRow['date']."',".$planRow['quantity'].",
         	                          ".$planRow['weight'].",".$planRow['processId1'].",".$planRow['processId2'].",".$planRow['processId3'].",
         	                          ".$planRow['noOfProcess'].",'".$temp1."','".$soaking1."','".$furnace1."','".$temp2."','".$soaking2."',
         	                          '".$furnace2."','".$temp3."','".$soaking3."','".$furnace3."',".$process1Reorder.",".$process2Reorder.",
         	                          ".$process3Reorder.",'inplanning', ".$_POST['planningId'].")";
      $insNewRecordRes = mysql_query($insNewRecord);
      $insertedId = mysql_insert_id();
      if($insNewRecordRes)
      {
      	$processNo = $_POST['processNo'];
      	$loopRecord = 0;
      	
      	if(isset($_POST['process3Reorder']))
      	{
      		$loopRecord = $loopRecord + 1;
      	}
      	
      	if(isset($_POST['process2Reorder']))
      	{
      		$loopRecord = $loopRecord + 1;
      	}
      	if(isset($_POST['process1Reorder']))
      	{
      		$loopRecord = $loopRecord + 1;
      	}
      	
      	for($i = 0; $i < $loopRecord; $i++)
      	{
      		$updateSheet = "UPDATE logsheet
      		                   SET status = -1
      		                 WHERE planningId = ".$_POST['planningId']."
        	                   AND processNo = ".$processNo;
          $updateSheetRes = mysql_query($updateSheet);
          $processNo = $processNo - 1;
      	}
        $selLogsheet = "SELECT incomingId, processId, processNo, planningId, logsheetNo, logsheetDate, startTime, startTemperature, 
                               soakingStartTime, endTime, endTemperature, quenchingMedia, quenchingTime, tempBforQue, tempAfterQue, status
                          FROM logsheet
                         WHERE planningId = ".$_POST['planningId']."
                           AND status = 1";
      	$selLogsheetRes = mysql_query($selLogsheet);
      	while($sheetRow = mysql_fetch_array($selLogsheetRes))
      	{
      		$insertNewSheet = "INSERT INTO logsheet(incomingId, processId, processNo, planningId, logsheetNo, logsheetDate, startTime, startTemperature, 
                                                  soakingStartTime, endTime, endTemperature, quenchingMedia, quenchingTime, tempBforQue, tempAfterQue, 
                                                  status)
                                  VALUES (".$sheetRow['incomingId'].",".$sheetRow['processId'].",".$sheetRow['processNo'].",".$insertedId.",
                                          '".$sheetRow['logsheetNo']."','".$sheetRow['logsheetDate']."','".$sheetRow['startTime']."',
                                          '".$sheetRow['startTemperature']."','".$sheetRow['soakingStartTime']."','".$sheetRow['endTime']."',
                                          '".$sheetRow['endTemperature']."','".$sheetRow['quenchingMedia']."','".$sheetRow['quenchingTime']."',
                                          '".$sheetRow['tempBforQue']."','".$sheetRow['tempAfterQue']."','".$sheetRow['status']."')";
          $insertNewSheetRes = mysql_query($insertNewSheet);
      	}
      	if($insertNewSheetRes)
      	{
      		$updateSheetLast = "UPDATE logsheet
      		                   SET status = -1
      		                 WHERE planningId = ".$_POST['planningId'];
          $updateSheetRes = mysql_query($updateSheetLast);
      	}
      }
    }
    if($insNewRecordRes)
    {
    	$updatePlanRecord = "UPDATE planning 
    	                        SET status = 'not ok'
    	                      WHERE planningId = ".$_POST['planningId'];
      $updatePlanRecordRes = mysql_query($updatePlanRecord);
    }
	  //Effecting of Planning Record : Ends
  }
  
  $logsArray = array();
	$i =0;
  $selectLogRecords = "SELECT logSheetId,incomingId,processNo,planningId,logsheetNo,DATE_FORMAT(logsheetDate,'%d-%m-%Y')AS logsheetDate,
                              DATE_FORMAT(startTime,'%h:%i %p')AS startTime,startTemperature,DATE_FORMAT(soakingStartTime,'%h:%i %p')AS soakingStartTime,
                              DATE_FORMAT(endTime,'%h:%i %p')AS endTime,endTemperature,quenchingMedia,
                              DATE_FORMAT(quenchingTime,'%h:%i %p')AS quenchingTime,tempBforQue,
                              tempAfterQue,status,processName
												 FROM logsheet
												 LEFT JOIN process ON logsheet.processId = process.processId
												WHERE status = '0' OR status = '1'
												ORDER BY logSheetId";                                     
															 
	$selectLogRecordsRes = mysql_query($selectLogRecords);
	while($logRow = mysql_fetch_array($selectLogRecordsRes))
	{
	  $logsArray[$i]['logSheetId']       = $logRow['logSheetId'];
	  $logsArray[$i]['incomingId']       = $logRow['incomingId'];
	  $logsArray[$i]['processNo']        = $logRow['processNo'];
	  $logsArray[$i]['processName']      = $logRow['processName'];
	  $logsArray[$i]['planningId']       = $logRow['planningId'];
	  $logsArray[$i]['logsheetNo']       = $logRow['logsheetNo'];
	  $logsArray[$i]['logsheetDate']     = $logRow['logsheetDate'];
	  $logsArray[$i]['startTime']        = $logRow['startTime'];
	  $logsArray[$i]['startTemperature'] = $logRow['startTemperature'];
	  $logsArray[$i]['soakingStartTime'] = $logRow['soakingStartTime'];
	  $logsArray[$i]['endTime']          = $logRow['endTime'];
	  $logsArray[$i]['endTemperature']   = $logRow['endTemperature'];
	  $logsArray[$i]['quenchingMedia']   = $logRow['quenchingMedia'];
	  $logsArray[$i]['quenchingTime']    = $logRow['quenchingTime'];
	  $logsArray[$i]['tempBforQue']      = $logRow['tempBforQue'];
	  $logsArray[$i]['tempAfterQue'] 	   = $logRow['tempAfterQue'];
	  $logsArray[$i]['status'] 				   = $logRow['status'];
	  $i++;
	}
  
//  $selPlanning = "SELECT planning.planningId,planning.incomingId,partyName,quantity,planning.weight,pro1.processName AS proName1,pro2.processName AS proName2,
//                         pro3.processName AS proName3, challan
//                    FROM planning
//                    JOIN incoming ON planning.incomingId = incoming.incomingId
//                    JOIN party ON planning.partyId = party.partyId
//                    JOIN process AS pro1 ON planning.processId1 = pro1.processId
//                    LEFT JOIN process AS pro2 ON planning.processId2 = pro2.processId
//                    LEFT JOIN process AS pro3 ON planning.processId3 = pro3.processId
//                   WHERE planning.status = 'log complete' ";
//  $selPlanningRes = mysql_query($selPlanning);
//  $plannArray = array();
//  $count = 0;
//  while($planRow = mysql_fetch_array($selPlanningRes))
//  {
//    $plannArray[$count]['planningId'] = $planRow['planningId'];
//    $plannArray[$count]['incomingId'] = $planRow['incomingId'];
//    $plannArray[$count]['challan']    = $planRow['challan'];
//    $plannArray[$count]['partyName']  = $planRow['partyName'];
//    $plannArray[$count]['quantity']   = $planRow['quantity'];
//    $plannArray[$count]['weight']     = $planRow['weight'];
//    $plannArray[$count]['proName1']   = $planRow['proName1'];
//    $plannArray[$count]['proName2']   = $planRow['proName2'];
//    $plannArray[$count]['proName3']   = $planRow['proName3'];
//    $count++; 
//  }



	include("./bottom.php");
	$smarty->assign("logsArray",$logsArray);
	//$smarty->assign("plannArray",$plannArray);
	$smarty->assign("msg",'<font size="6">No Record Found</font>');
	$smarty->display("logsheetList.tpl");
}
?>
